#include "CCSkin.h"
#include "CCSlot.h"

NS_CC_BEGIN

CCSkin::CCSkin() {
    
}

CCSkin::~CCSkin() {
    for (auto iter:m_slotMap) {
        iter.second->release();
    }
}

CCSkin* CCSkin::create() {
    CCSkin* s = new CCSkin();
    return (CCSkin*)s->autorelease();
}

void CCSkin::addSlot(CCSlot* slot, CCAttachment* attachment) {
    auto iter=m_slotMap.find(slot);
    if (iter==m_slotMap.end()) {
        CCArray* ary=CCArray::create();
        ary->retain();
        iter=m_slotMap.insert(make_pair(slot,ary)).first;
    }
    iter->second->addObject(attachment);
}

CCAttachment* CCSkin::getAttachment(CCSlot* slot, const char* attachmentName) {
    auto iter=m_slotMap.find(slot);
    if(iter!=m_slotMap.end()) {
        CCArray* ary=iter->second;
        for (int i=0;i<ary->count();i++) {
            CCAttachment* attachment=(CCAttachment*)ary->objectAtIndex(i);
            if (!strcmp(attachment->getName(), attachmentName)) {
                return attachment;
            }
        }
    }
    return NULL;
}

CCAttachment* CCSkin::getFirstAttachment(CCSlot* slot) {
	CCAttachment* ret = nullptr;

	auto iter = m_slotMap.find(slot);
	if(iter != m_slotMap.end()) {
		CCArray* arr = iter->second;
		if(arr->count() > 0) {
			ret = (CCAttachment*)arr->objectAtIndex(0);
		}
	}

	return ret;
}

NS_CC_END